<?php


namespace app\admin\model;


use app\lib\exception\MysqlErrorException;
use Driver\Cache\Redis;

class Sign extends BaseModel
{
    # 根据user_id获取所有的sign
    public function getAllStatus($user_id)
    {
        return self::where('user_id', '=', $user_id)
            ->select()
            ->toArray();
    }

    # 根据user_id获取对应的信息
    public function getSign($sign_id)
    {
        return self::where('sign_id', '=', $sign_id)
            ->find()
            ->toArray();
    }

    # 更新sign
    public function updateSign($data)
    {
        array_key_exists('status', $data) ? $data['status'] = 1 : $data['status'] = 0;
        unset($data['sign']);
        $id = $data['id'];
        unset($data['id']);
        $data['update_time'] = now_time();
        $re = self::where('sign_id', '=', $id)
            ->update($data);
        # 同步等下到redis
        $arr = self::where('sign_id', '=', $id)
            ->where('status', '=', 1)
            ->column('sign');
        $model = new Account();
        $user_id = self::where('sign_id', '=', $id)
            ->find();
        $info = $model->get_name($user_id['user_id']);

        $redis_key = config('redis.all_sign').$info['name'];
        Redis::sAddArr($redis_key, $arr);
        return [
            'code' => 200,
            'msg' => '修改成功',
        ];

    }

    # 批量禁止sign
    public function disableSign($ids)
    {
        return self::where('sign_id', 'in', $ids)
            ->update(['status'=>0]);
    }

    # 批量取消禁止sign
    public function noDisableSign($ids)
    {
        return self::where('sign_id', 'in', $ids)
            ->update(['status'=>1]);
    }

}